

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

import javax.tools.DiagnosticCollector;
import javax.tools.DiagnosticListener;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;

import engine.Engine;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			
			new Main();
		} catch (Exception e) {
			System.out.println("Error");
			e.printStackTrace();
		}
	}

	public static final int MAX = 100;
	public static final boolean DEBUG = false;
	
	public String bot1, bot2;
	public int tie, p1, p2;
	
	PrintStream out_orig;
	PrintStream err_orig;
	
	ByteArrayOutputStream err_baos;
    PrintStream err_new;
    
    ByteArrayOutputStream out_baos;
    PrintStream out_new;
    
	Main() throws Exception{
		//compile();
		out_orig = System.out;
		err_orig = System.err;
		
		if(!DEBUG) setNewSystem();
		
		
//		bot1 = "BullyBot";
//		bot1 = "BreadthFirst";
//		bot1 = "AlphaBeta";
		bot1 = "Adaptive1";
//		bot1 = "Adaptive2";
		
		
//		bot2 = "BreadthFirst";
//		bot2 = "AlphaBeta";
//		bot2 = "Adaptive1";
		bot2 = "Adaptive2";
		
		
		
		tie = p1 = p2 = 0;

		
		
		String folder, map;
		//int count = 0;
		for(int i = 3; i <= 8; i++){
			folder = i+"planets";
			for(int j = 1; j <= 3; j++){
				map = "map"+j;
				//for(int k = 0; k<3;k++){
					run(folder, map, bot1, bot2);
				//}
				
				if(!DEBUG) printWinner(err_baos);
				//if(++count == MAX) break;
			}
			//if(count == MAX) break;
		}
		
		setSystem(out_orig, err_orig);
		
		System.out.println("# of ties:    "+ tie);
		System.out.println("# of wins b1: "+ p1);
		System.out.println("# of wins b2: "+ p2);
	}
	@SuppressWarnings("unused")
	private void compile() {
		try {
			Runtime.getRuntime().exec("javac *.java");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void setNewSystem() {
		out_baos = new ByteArrayOutputStream();
		err_baos = new ByteArrayOutputStream();
		
		out_new = new PrintStream(out_baos);
		err_new = new PrintStream(err_baos);
		
		setSystem(out_new, err_new);
	}
	
	private void setSystem(PrintStream out, PrintStream err) {
		System.setOut(out);
		System.setErr(err);
	}

	private void printWinner(ByteArrayOutputStream baos) {
		String s = baos.toString();
		
		setSystem(out_orig, err_orig);
		
		if(s.toLowerCase().contains("draw")){
			tie++;
			System.out.println("Draw");
		}
		else if(s.toLowerCase().contains("1 wins")){
			p1++;
			System.out.println("Bot 1 Wins");
		}
		else if(s.toLowerCase().contains("2 wins")){
			p2++;
			System.out.println("Bot 2 Wins");
		}
			
		if(!DEBUG) setNewSystem();
		
	}

	

	private void run(String folder, String map, String bot1, String bot2) throws Exception{
		List<String> arr = new ArrayList<String>();
		arr.add("src/tools/maps/"+folder+"/"+map+".txt");
		arr.add("java "+bot1);
		arr.add("java "+bot2);
		arr.add("parallel");
		arr.add("100");
		arr.add("4000");
		String[] stringArray = Arrays.copyOf(arr.toArray(), arr.toArray().length, String[].class);
		
		engine.Engine.main(stringArray);
	}

	@SuppressWarnings("unused")
	private String SToString(InputStream is){
		Scanner sc = new Scanner(is);
		StringBuilder sb = new StringBuilder();
		sc.useDelimiter(":");
		while(sc.hasNext()){
			sb.append(sc.next());
		}
		sc.close();
		return sb.toString();
	}

	@SuppressWarnings("unused")
	private String ISToString(InputStream is) throws IOException{
		Scanner sc = new Scanner(is);
		sc.useDelimiter(":");
		while(sc.hasNext()){
			String tmp = sc.next();
			if(tmp.toLowerCase().contains("wins")){
				sc.close();
				return tmp.split("!")[0];
			}
		}
		sc.close();
		return "No Result";
	}

}
